package com.angel.oss;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;

/**
 * @DESC 阿里云文件下载测试
 * @Author zhangzhenhua
 * @Date 2017年7月25日 下午2:44:33
 */
public class FileDownLoad {

	// TODO Auto-generated method stub
	// endpoint以杭州为例，其它region请按实际情况填写
	static String endpoint = "http://oss-cn-qingdao.aliyuncs.com";
	// 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录
	// https://ram.console.aliyun.com 创建
	static String accessKeyId = "LTAIH0ihGo2wWa0U";
	static String accessKeySecret = "YCEUufhLNc135ZqtdapkR0tYz3gs77";
	static String bucketName = "tianshidata";

	// http://tianshidata.oss-cn-qingdao.aliyuncs.com/test/java-123.doc
	public static void main(String[] args) throws Exception {
		String key = "test/java-123.doc";
		String des = "c:/tmp/java-123-2.doc";
		DownLoadFile(key, des);
	}

	public static File DownLoadFile(String key, String des) throws Exception {
		long t1 = System.currentTimeMillis();
		// 创建OSSClient实例
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		OSSObject ossObject = ossClient.getObject(bucketName, key);
		File file = new File(des);
		InputStream is = ossObject.getObjectContent();
		OutputStream os = new FileOutputStream(file);
		byte[] b = new byte[10000];
		int bytesRead = 0;
		while ((bytesRead = is.read(b)) != -1) {
			os.write(b, 0, bytesRead);
		}
		os.flush();
		os.close();
		is.close();
		System.out.println((System.currentTimeMillis() - t1) + "ms");
		return file;
	}

	private void uploadTest(String path) throws Exception {
		long t1 = System.currentTimeMillis();
		File handler = new File(path);
		File file2 = new File("c:/sudoTest.doc");
		InputStream is = new FileInputStream(handler);
		OutputStream os = new FileOutputStream(file2);
		byte[] b = new byte[10000];
		int bytesRead = 0;
		while ((bytesRead = is.read(b)) != -1) {
			os.write(b, 0, bytesRead);
		}
		os.flush();
		os.close();
		is.close();
		System.out.println("大小：" + handler.length() / 1000 + "kb;  上传时间：" + (System.currentTimeMillis() - t1) + " ms;");
	}

	/**
	 * @param key
	 *            "test/java-123.doc"
	 * @throws IOException
	 */
	public void fileReader(String key) throws IOException {

		// 创建OSSClient实例
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		OSSObject ossObject = ossClient.getObject(bucketName, key);
		// 读Object内容
		System.out.println("Object content:");
		BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
		while (true) {
			String line = reader.readLine();
			if (line == null)
				break;
			System.out.println("\n" + line);
		}
		reader.close();
		ossObject.getObjectContent().close();
		// 关闭client
		ossClient.shutdown();

	}

}
